'use strict'

define(['jquery', 'bootstrap', 'select2', 'shiftform', 'validate', 'validate.en'], function($) {
  
  return {
        
    init: function() {
        this.create();
        this.delete();
    },
    
    create: function() {
    	var that = this;
        $(document).on('shown.bs.modal', '#agent-create', function(e){
        	
        	that.bindUserSearchClick(e);
        	
        	that.bindTypeChange(e);
        	
        	var $userDiscountValidate = {'discount_validate' : function(value) { return that.validateDiscount(value); }}
        	$.extend($.bt_validate.fn, $userDiscountValidate)
        	$.extend($.bt_validate.text, {'discount_validate' : '请输入数字格式,中间以英文逗号隔开!'})
        	
        	var $percentageValidate = {'percentage_validate' : function(value) { return that.validatePercentage(value); }}
        	$.extend($.bt_validate.fn, $percentageValidate)
        	$.extend($.bt_validate.text, {'percentage_validate' : '请输入小于100%的数字!'})

	    	$(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
	        
	        var $form = $(e.currentTarget).find("form")                          //bind shiftform
	        $form.shiftform({
	          beforeSubmit : function(el) {
	        	$form.trigger("validate.ajax.submit");
	            return $.bt_validate.result[$.bt_validate.form_id];
	          }
	        })
        })
      },
      
      validateDiscount: function(value) {
    	  var result = true;
    	  
		  if(value != '') {
			  var offers = value.split(',');
    		  for(var j=0; j<offers.length; j++) {
	          	if(isNaN(offers[j])){
	          		result = false;
	          	}
	          }
		  }
          return result;
      },
      
      validatePercentage: function(value) {
    	  var result = true;
    	  
		  if(value != '') {
			  var offers = value.split(',');
    		  for(var j=0; j<offers.length; j++) {
	          	if(offers[j] > 100){
	          		result = false;
	          	}
	          }
		  }
          return result;
      },
      
      bindUserSearchClick: function(e) {
    	  $(e.currentTarget).find('.user-search').click(function() {
      		var mobile = $(e.currentTarget).find('input[name="mobile"]').val()
      		if(mobile != '') {
      			$.ajax({
          			url: window.__CTX__.root + "/agents",
          			type: 'GET',
          			data: {"filters": "true", "mobile": mobile},
          			cache: false
          		}).done(function(result) {
          			result = JSON.parse(result);
          			$(e.currentTarget).find('.search-block').css("display", "");
          			if(result.code == '0') {
          				$(e.currentTarget).find('input[name="user.id"]').val(result.data.userId);
          				$(e.currentTarget).find('input[name="name"]').val(result.data.username);
          				$(e.currentTarget).find('.search-result').text(result.data.username);
          				$(e.currentTarget).find('.btn-success').removeClass("disabled")
          			} else if(result.code == '1') {
          				$(e.currentTarget).find('.search-result').text(result.message);
          				$(e.currentTarget).find('.btn-success').addClass("disabled")
          			}
          		})
      		}
      		 
      	})
      },
      
      bindTypeChange: function(e) {
    	var $discountType = $('select[name="discountType"]');
      	$discountType.change(function() {
      		var $discount = $(".discount-type");
          	var $payment = $(".payment-type");
          	if($discountType.val() === 'discount') {
          		$discount.css("display", "none");
          		$('input[name="discountOffer"]').removeAttr("data-validate").val("");
          		$('input[name="commission"]').removeAttr("data-validate").val("");
          		
          		$payment.css("display", "");
          		$('input[name="agentDiscount"]').attr("data-validate", "required|number|percentage_validate");
          		$('input[name="userDiscount"]').attr("data-validate", "required|discount_validate|percentage_validate");
          	} else if($discountType.val() == 'payment') {
          		$discount.css("display", "");
          		$('input[name="discountOffer"]').attr("data-validate", "required|discount_validate");
          		$('input[name="commission"]').attr("data-validate", "required|discount_validate");
          		$payment.css("display", "none");
          		$('input[name="agentDiscount"]').removeAttr("data-validate").val("");
          		$('input[name="userDiscount"]').removeAttr("data-validate").val("");
          	}
      	})
      },
      
      delete: function() {
        $(document).on('shown.bs.modal', '#agent-delete', function(e){
          $(e.currentTarget).find("form").shiftform() 
        })
      }
     
  }
})
